package com.android.settings.ios;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.settings.ios.general.GeneralFragment;

public class SettingsMainActivity extends FragmentActivity {

    private TextView mTvMainTitle;
    private TextView mTvBackText;
    private TextView mTvHeaderTitle;

    private LinearLayout mBarMain;
    private LinearLayout mBarSub;

    private int mBackgroundColor = Color.WHITE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OsUtils.init(getApplication());

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        setContentView(R.layout.os_activity_settings_main);

        mBackgroundColor = getColor(R.color.os_preference_bg_color_normal);

        if (savedInstanceState == null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(R.id.fragment_container, new GeneralFragment(getString(R.string.general)))
                    .add(R.id.fragment_header, new SettingsFragment())
                    .commit();
        }

        mBarMain = findViewById(R.id.layout_bar_main);
        mBarSub = findViewById(R.id.layout_bar_sub);

        mTvHeaderTitle = findViewById(R.id.tv_header_title);
        mTvBackText = findViewById(R.id.tv_fragment_back_title);
        mTvBackText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mTvMainTitle = findViewById(R.id.tv_fragment_main_title);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);
            Log.e("wss", "fragment="+fragment);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        if (mTvMainTitle != null) {
            mTvMainTitle.setText(titleId);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (mTvMainTitle != null) {
            mTvMainTitle.setText(title);
        }
    }

    public void setLeftBackText(String title) {
        if (title != null && title.length() > 0) {
            mTvBackText.setText(title);
            mTvBackText.setVisibility(View.VISIBLE);
        } else {
            mTvBackText.setVisibility(View.INVISIBLE);
        }
    }

    public void updateSubBarBackgroundAlpha(float alpha) {
        if (alpha >= 0.9f) {
            mBarSub.setBackgroundColor(mBackgroundColor);
            mBarSub.setElevation(2.0f);
        } else {
            mBarSub.setBackgroundColor(Color.TRANSPARENT);
            mBarSub.setElevation(0.0f);
        }
    }

    public void updateMainBarBackgroundAlpha(float alpha) {
        if (alpha >= 0.9f) {
            mBarMain.setBackgroundColor(mBackgroundColor);
            mBarMain.setElevation(2.0f);
            mTvHeaderTitle.animate().alpha(1.0f).start();
        } else {
            mBarMain.setBackgroundColor(Color.TRANSPARENT);
            mBarMain.setElevation(0.0f);
            mTvHeaderTitle.animate().alpha(0.0f).start();
        }
    }

    @Override
    public void onBackPressed() {
        int size = getSupportFragmentManager().getBackStackEntryCount();
        if (size < 1) {
            finish();
        } else {
            super.onBackPressed();
        }
    }
}